package com.dcbeida.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.bean.DrugInfo;
import com.dcbeida.bean.Drugorderinfo;
import com.dcbeida.bean.ResultModel;
import com.dcbeida.bean.Result_Api;
import com.dcbeida.bean.User;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.utils.GenApiHashUrl;
import com.dcbeida.utils.GetApi;
import com.dcbeida.utils.JsonGet;

import butterknife.Bind;
import dc.medicine.com.okhttplib.OkHttpUtils;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class ResultActivity extends BaseActivity {


    private String code = "";

    private String msg = "";

    @Bind(R.id.tv_value)
    TextView tv_value;

    @Bind(R.id.tv_message)
    TextView tv_message;

    @Bind(R.id.tv_query)
    TextView tv_query;
    @Bind(R.id.lin_next)
    LinearLayout lin_next;

    @Bind(R.id.img_back)
    ImageView img_back;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_result;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        if (intent != null) {
            if (intent.hasExtra("code"))
                code = intent.getStringExtra("code");
        }

        setupview();

    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    private void setupview() {
        tv_value.setText(code);
        tv_message.setText("");

        tv_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //new getPayTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, code);

                OkHttpUtils.post(GenApiHashUrl.apiUrl).tag(this)
                        .paramsApp("service","querydrugorderinfo")
                        .paramsApp("params", new JsonGet().putopt("drugcode", code)
                                .putopt("uid",MyApplication.user.getUid())
                                .tostring()).execute(new MethodCallBack(ResultActivity.this,Drugorderinfo.class,listener));

            }
        });

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ResultActivity.this.finish();
            }
        });

        lin_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showActivity(CaptureActivity.class,null);
                ResultActivity.this.finish();
            }
        });
    }







     final class MethodCallBack<T> extends DialogCallback<T> {

        public MethodCallBack(Activity activity, Class<String> clazz,DialogInterface.OnCancelListener listener) {
            super(activity, clazz,listener);
        }

        @Override
        public void onResponse(T data) {
            if(data!=null) {
                if (data instanceof Drugorderinfo) {
                    tv_message.setText(((Drugorderinfo) data).getResultdesc());
                }
            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            showTip("网络加载有问题，请重试");

        }
    }

    DialogInterface.OnCancelListener listener= new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            OkHttpUtils.getInstance().cancelTag(this);
        }
    };


}
